package view;
import control.LayDuLieuDiem;
import fhedu.Cell;
import fhedu.CellPH;
import fhedu.FHEDU;
import fhedu.Mau;
import fhedu.MyFont;
import fhedu.menu;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import global.*;
import obj.objResponseScore;
/**
 *
 * @author kUN
 */
public class XemDiem extends GameCanvas implements CommandListener{
    //Vector<String> mon1 = new Vector<String>();
    int rong=getWidth();
    int cao=getHeight();
    int x=80,y=80;
    Image bangdiem=Image.createImage(380, 320);
    Command cmdQuaylai=new Command("Quay lại", Command.CANCEL, 0);
    Command cmdQLmenu=new Command("Quay lại", Command.CANCEL, 0); 
    Command cmdGui=new Command("Gửi", Command.CANCEL, 1);    
  //  Command cmdTh=new Command("Thoát", Command.CANCEL, 0);
    Graphics gbangdiem=bangdiem.getGraphics();
    Graphics gxemdiem=getGraphics();
    int socot=6,sohang=8;
    Cell cbangdiem;
    CellPH phanhoi[] =new CellPH[sohang-1];
    int vtfocus=0;
    TextBox ph=new TextBox("Phản hồi", null, 255, TextField.ANY);
    //---------------------------------------------------------------------------------------------------
    String[] mon;
    String[] chuyencan;
    String[] dieukien;
    String[] thi;
    String[] tongket;
    //double number = (double)(int)((8.00001+0.005)*100.0)/100.0;
    //---------------------------------------------------------------------------------------------------
    public XemDiem(){
        super(false);        
        this.addCommand(cmdQLmenu);
        this.setCommandListener(this);
//--ket noi
        objResponseScore response=new objResponseScore();
        //response=LayDuLieuDiem.LayDuLieu(CurrentUserInfor.nguoiDungDanngNhap.getPK_sNguoidungID(), Global.kyhoc+"");
        response=LayDuLieuDiem.LayDuLieu("SV08B602",Global.kyhoc+"");
        sohang=response.getObjDiems().length+1;
        mon=new String[sohang];
        mon[0]="Môn Học";
        chuyencan=new String[sohang];
        chuyencan[0]="Chuyên Cần";
        dieukien=new String[sohang];
        dieukien[0]="Điều Kiện";
        thi=new String[sohang];
        thi[0]="Điểm Thi";
        tongket=new String[sohang];
        tongket[0]="Tổng Kết";
       // tongket[0]=""+number;
        double dtongket=0;
        for(int i=1;i<sohang;i++)
        {
            mon[i]=response.getObjDiems()[i-1].getsMon();
            chuyencan[i]=response.getObjDiems()[i-1].getfDiemchuyencan()+"";
            dieukien[i]=response.getObjDiems()[i-1].getfDiemdieukien()+"";
            thi[i]=response.getObjDiems()[i-1].getfDiemthi()+"";
            dtongket=response.getObjDiems()[i-1].getfDiemchuyencan()/10+response.getObjDiems()[i-1].getfDiemdieukien()/5+(response.getObjDiems()[i-1].getfDiemthi()*7/10);
            dtongket=(double)(int)((dtongket+0.005)*100.0)/100.0;
            tongket[i]=dtongket+"";
        }
        
        //------------------------
        
        
       // sohang=getSoMon()+1;

        VeBangDiem();
        gxemdiem.drawImage(bangdiem, 0, 0, Graphics.TOP|Graphics.LEFT);
        this.Scrall(gxemdiem);
        flushGraphics();
    }
    
    public void refresh(Graphics g)
    {
        //gxemdiem.drawImage(bangdiem, x, y, Graphics.TOP|Graphics.LEFT);
        gxemdiem.drawRegion(bangdiem, x, y, getWidth()-80, 80+sohang*20, Sprite.TRANS_NONE, 80, 80, Graphics.TOP|Graphics.LEFT);
        this.Scrall(gxemdiem);
        flushGraphics();        
        
    }
    public void Scrall(Graphics g)
    {
         //ve tro
         g.setColor(0x808080);
         g.fillRect(0, 80+sohang*20, 380, 10);
         g.setColor(0x4e4d4d);
         g.fillTriangle(82, 80+sohang*20+5, 86, 80+sohang*20+1, 86, 80+sohang*20+9);
         g.fillTriangle(getWidth()-2, 80+sohang*20+5, getWidth()-6, 80+sohang*20+1, getWidth()-6, 80+sohang*20+9);
         g.fillRect(90,80+sohang*20+1 , 50, 8);
    }

    public void VeBangDiem()
    {
       // this.setTicker(tf);
        
        gbangdiem.setColor(Mau.maunenct);
        gbangdiem.fillRect(0, 0, 400, 320);
        gbangdiem.setFont(MyFont.fontto);
        gbangdiem.setColor(0x3db2d8);
        gbangdiem.drawString("FITHOU EDU", rong/2, 2, Graphics.TOP|Graphics.HCENTER);
        gbangdiem.setFont(MyFont.fontnhonghieng);        
      //  gbangdiem.drawString("Ghi chú: Sinh viên có thắc mắc về điểm", 10, 250, Graphics.TOP|Graphics.LEFT);
      //  gbangdiem.drawString("xin mời nhấn vào nút         của môn học", 10, 265, Graphics.TOP|Graphics.LEFT);
      //  gbangdiem.drawString("để liên hệ với thầy cô bộ môn.", 10, 280, Graphics.TOP|Graphics.LEFT);
        try{
        Image lhe=Image.createImage("/lienhe.png");
       
        gbangdiem.drawImage(lhe, 130, 263, Graphics.TOP|Graphics.LEFT );
        }catch(Exception e){};
        
        gbangdiem.setFont(MyFont.fontnhodam);
        gbangdiem.drawString("Sinh viên: Bùi Thanh Minh", 50, 30, Graphics.TOP|Graphics.LEFT );
        gbangdiem.drawString("Lớp: 08B6", 50, 50, Graphics.TOP|Graphics.LEFT);
        gbangdiem.drawString("Học Kỳ: 7", 120, 50, Graphics.TOP|Graphics.LEFT);
        
        //gbangdiem.setFont(MyFont.fontnhonghieng);        
       //"Môn học"
       // String[] mon= {"Môn học","LT JaVa","Asembler","LT Web","QL Dự Án","ĐLCM","SQL","Giải Tích"};
        //cot môn
         for(int i=0;i<sohang;i++)
         {
           cbangdiem=new Cell(mon[i], 80, 20, Mau.mauden, Mau.mauden, 0xb2b0b0);           
           if(i>0)
           {
               gbangdiem.setFont(MyFont.fontnho);
               
               cbangdiem.canle=0;
           }
           cbangdiem.VeCell(gbangdiem, 0, 80+i*20);
         } 
         
         //cột điểm chuyên cần
         gbangdiem.setFont(MyFont.fontnhodam);   
       //  String[] chuyencan= {"ChuyênCần","10","10","8","10","8","6","10"};
         for(int i=0;i<sohang;i++)
         {
           cbangdiem=new Cell(chuyencan[i], 70, 20, Mau.mauden, Mau.mauden, Mau.mautrang);           
           if(i>0)
           {
               gbangdiem.setFont(MyFont.fontnho);
               cbangdiem.canle=2;
           }
           cbangdiem.VeCell(gbangdiem, 80, 80+i*20);
         } 
         
         //cột điểm điều kiện
         gbangdiem.setFont(MyFont.fontnhodam);   
         //String[] dieukien= {"ĐiềuKiện","9","9","10","10","10","9","10"};
         for(int i=0;i<sohang;i++)
         {
           cbangdiem=new Cell(dieukien[i], 60, 20, Mau.mauden, Mau.mauden, Mau.mautrang);           
           if(i>0)
           {
               gbangdiem.setFont(MyFont.fontnho);
               cbangdiem.canle=2;
           }
           cbangdiem.VeCell(gbangdiem, 150, 80+i*20);
         }         
         
         //điểm thi
         gbangdiem.setFont(MyFont.fontnhodam);   
        // String[] thi= {"ĐiểmThi","10","10","8","10","8","10","10"};
         for(int i=0;i<sohang;i++)
         {
           cbangdiem=new Cell(thi[i], 60, 20, Mau.mauden, Mau.mauden, Mau.mautrang);           
           if(i>0)
           {
               gbangdiem.setFont(MyFont.fontnho);
               cbangdiem.canle=2;
           }
           cbangdiem.VeCell(gbangdiem, 210, 80+i*20);
         } 
         
         //điểm tổng kết
         gbangdiem.setFont(MyFont.fontnhodam); 
       //  String[] tongket= {"TổngKết","10","10","9","10","8","10","10"};
         for(int i=0;i<sohang;i++)
         {
           cbangdiem=new Cell(tongket[i], 60, 20, Mau.mauden, Mau.mauden, Mau.mautrang);           
           if(i>0)
           {
               gbangdiem.setFont(MyFont.fontnho);
               cbangdiem.canle=2;
           }
           cbangdiem.VeCell(gbangdiem, 270, 80+i*20);
         } 
         
         //Liên hệ
         gbangdiem.setFont(MyFont.fontnhodam); 
         cbangdiem=new Cell("LiênHệ", 50, 20, Mau.mauden, Mau.mauden, Mau.mautrang);
         cbangdiem.VeCell(gbangdiem, 330, 80);
         
         for(int i=1;i<sohang;i++)
         {
           phanhoi[i-1]=new CellPH("Image/lienhe.png", 50, 20, Mau.mauden, Mau.mauden, Mau.mautrang); 
           phanhoi[i-1].x=330;
           phanhoi[i-1].y=80+i*20;
           phanhoi[i-1].IsImage=1;        
           phanhoi[i-1].VeCell(gbangdiem, 330, 80+i*20);
           
         } 
         phanhoi[0].Focus(gbangdiem);
             
 
         //bangdiem=null;
    }
    public void keyPressed(int k)
    {
        switch(getGameAction(k))
        {
            case RIGHT:
                if(x<220)
                {
                x=x+10;
                this.refresh(gxemdiem);
                }
                break;
            case LEFT:
                if(x>80)
                {
                x=x-10;
                this.refresh(gxemdiem);
                }
                break;
            case DOWN:
                if(vtfocus<sohang-2)
                {
                phanhoi[vtfocus].UnFocus(gbangdiem);
                vtfocus+=1;
                phanhoi[vtfocus].Focus(gbangdiem);
                this.refresh(gxemdiem);
                }
                break;
            case UP:
                if(vtfocus>0)
                {
                phanhoi[vtfocus].UnFocus(gbangdiem);
                vtfocus-=1;
                phanhoi[vtfocus].Focus(gbangdiem);
                this.refresh(gxemdiem);
                }
                break;        
            case FIRE:

                if(vtfocus==0)
                {
                    ph.addCommand(cmdQuaylai);
                    ph.addCommand(cmdGui);
                    ph.setCommandListener(this);        
                    FHEDU.display.setCurrent(ph);
                 
                } 
                
                break;

                
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c==cmdQuaylai)
        {
            
            FHEDU.display.setCurrent(this);
            
        }
         if(c==cmdQLmenu)
        {
                //gan cac string = null nữa
                bangdiem=null;  
                gbangdiem=null;
                gxemdiem=null;   
                cbangdiem=null;
                phanhoi=null; 
                cmdGui=null;
                cmdQLmenu=null;
                cmdQuaylai=null;
                ph=null;   
                
            FHEDU.display.setCurrent(new menu());
            
        }
        
    }
}